<?php

namespace app\api\controller;
use app\common\controller\Api;
use app\common\model\Teacher as TeacherModel;
use app\common\model\TeacherOrder as TeacherOrderModel;
use app\common\model\User as UserModel;
use app\common\model\UserCoupons;
use app\common\model\UserCouponsLog;
use think\Db;

class TeacherOrder extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];


    //退款
    public function refund()
    {
        $param = $this->request->post();
        $user_id = $this->auth->id;
        $userCoupons = new UserCoupons();
        $userCouponsLog = new UserCouponsLog();
        $row = TeacherOrderModel::where([
            'id'=>$param['id'],
            'user_id'=>$user_id
        ])->find();
        if(!$row){
            $this->error('订单不存在');
        }
        if($row['status']!==1){
            $this->error('订单状态不正确');
        }
        Db::startTrans();
        try {
            $res1 = $row->allowField(true)->save(['status'=>-3,'refundtime'=>time()]);
            $res2 = $userCoupons->where([
                'id' => $row['user_coupons_id'],
                'user_id' => $row['user_id'],
            ])->setInc('nums',1);
            $userCouponsLog->save([
                'nums' => 1,
                'price' => $row['money'],
                'user_id' => $row['user_id'],
                'type' => 'back',
                'order_sn' => $row['order_sn'],
            ]);
            if($res1 && $res2){
                Db::commit();
            }else{
                Db::rollback();
            }
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }


        $this->success('退券成功');
    }

    //家长点击未完成试课，订单进入调节状态
    public function abnormal()
    {
        $param = $this->request->param();
        $user_id = $this->auth->id;
        if(!$param['abnormal']){
            $this->error('请大概描述试课情况');
        }
        $find = TeacherOrderModel::where([
            'id'=>$param['id'],
            'user_id'=>$user_id
        ])->find();

        if(!$find || $find['status']!==2){
            $this->error('没有找到相关订单');
        }

        $find->abnormal = $param['abnormal'];
        $find->save();

        $this->success('稍后会有工作人员联系您，请保持电话通畅！');
    }


    //教师-拒绝订单
    public function refuse()
    {
        $param = $this->request->param();
        $user_id = $this->auth->id;
        if(!$param['refund_reason']){
            $this->error('请填写拒绝原因');
        }
        $find = TeacherOrderModel::where([
            'id'=>$param['id'],
            'teacher_id'=>$user_id
        ])->find();
        if(!$find){
            $this->error('没有找到相关订单');
        }
        $find->status = -2;
        $find->refund_reason = $param['refund_reason'];
        $find->save();

        $weekdays = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
        $teacher = TeacherModel::where([
            'user_id' => $user_id,
        ])->find();

        $give_time = json_decode($teacher['give_time'],true);
        $per_week_key = array_search($find['per_week'], $weekdays);
        if($find['per_week_timesec'] && $give_time[$per_week_key][$find['per_week_timesec']]==''){
            $give_time[$per_week_key][$find['per_week_timesec']] = $find['per_week_time'];
            $give_time = json_encode($give_time,true);
            $teacher->give_time = $give_time;
            $teacher->save();
        }

        //每次拒绝接单减半颗星
        $params['star'] = -0.5;
        $params['user_id'] = $user_id;
        $params['type'] = '拒绝接单';
        \think\Hook::listen('teacher_star_change', $params);

        $this->success('success');
    }

    //普通用户订单
    public function user_order(){
        $param = $this->request->param();
        $user_id = $this->auth->id;
        $lists = TeacherOrderModel::where([
            'status' => $param['status'],
            'user_id' => $user_id,
        ])->order('id desc')->paginate(10);

        foreach($lists as $k=>&$v){
            $student = UserModel::where(['id'=>$v['user_id']])->find();
            $v['addr'] = explode(',',$student['addr'])[0];
            $v['student'] = $student['username'];
            $v['sex'] = $student['sex'];
            $v['state'] = $student['state'];
            if($v['status']>=2){
                $teacher = UserModel::where(['id'=>$v['teacher_id']])->find();
                $v['teacher_name'] = $teacher['nickname'];
                $v['teacher_mobile'] = $teacher['mobile'];
            }
        }
//        $lists['test'] = [
//            'status' => $param['status'],
//            'user_id' => $user_id,
//        ];
        $this->success('success',$lists);
    }

    //教师订单
    public function teacher_order(){
        $param = $this->request->param();
        $user_id = $this->auth->id;
        $lists = TeacherOrderModel::where([
            'status' => $param['status'],
            'teacher_id' => $user_id,
        ])->order('id desc')->paginate(10);

        foreach($lists as $k=>&$v){
            $student = UserModel::where(['id'=>$v['user_id']])->find();
            $v['addr'] = explode(',',$student['addr'])[0];
            $v['student'] = $student['username'];
            $v['sex'] = $student['sex'];
            $v['state'] = $student['state'];
            if($v['status']>=2) {
                $v['mobile'] = $student['mobile'];
            }
        }
        $this->success('success',$lists);
    }
}